package com.web.dao;

import com.web.entity.Role;
import com.web.entity.RoleExample;
import java.util.List;

import com.web.utils.mybatis.SimpleSelectInLangDriver;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

/**
 * @author FDjavaone
 */
public interface RoleMapper {
    @SelectProvider(type=RoleSqlProvider.class, method="countByExample")
    int countByExample(RoleExample example);

    @DeleteProvider(type=RoleSqlProvider.class, method="deleteByExample")
    int deleteByExample(RoleExample example);

    @Delete({
        "delete from t_role",
        " where id = #{id,jdbcType=BIGINT}"
    })
    int deleteByPrimaryKey(Long id);

    @Insert({
        "insert into t_role (code, text, ",
        " enabled, description, ",
        " create_time, modify_time)",
        " values (#{code,jdbcType=VARCHAR}, #{text,jdbcType=VARCHAR}, ",
        " #{enabled,jdbcType=BIT}, #{description,jdbcType=VARCHAR}, ",
        " #{createTime,jdbcType=TIMESTAMP}, #{modifyTime,jdbcType=TIMESTAMP})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insert(Role record);

    @InsertProvider(type=RoleSqlProvider.class, method="insertSelective")
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Long.class)
    int insertSelective(Role record);

    @SelectProvider(type=RoleSqlProvider.class, method="selectByExample")
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="code", property="code", jdbcType=JdbcType.VARCHAR),
        @Result(column="text", property="text", jdbcType=JdbcType.VARCHAR),
        @Result(column="enabled", property="enabled", jdbcType=JdbcType.BIT),
        @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP)
    })
    List<Role> selectByExample(RoleExample example);

    @Select({
        "select",
        " id, code, text, enabled, description, create_time, modify_time",
        " from t_role",
        " where id = #{id,jdbcType=BIGINT}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.BIGINT, id=true),
        @Result(column="code", property="code", jdbcType=JdbcType.VARCHAR),
        @Result(column="text", property="text", jdbcType=JdbcType.VARCHAR),
        @Result(column="enabled", property="enabled", jdbcType=JdbcType.BIT),
        @Result(column="description", property="description", jdbcType=JdbcType.VARCHAR),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="modify_time", property="modifyTime", jdbcType=JdbcType.TIMESTAMP)
    })
    Role selectByPrimaryKey(Long id);

    @UpdateProvider(type=RoleSqlProvider.class, method="updateByExampleSelective")
    int updateByExampleSelective(@Param("record") Role record, @Param("example") RoleExample example);

    @UpdateProvider(type=RoleSqlProvider.class, method="updateByExample")
    int updateByExample(@Param("record") Role record, @Param("example") RoleExample example);

    @UpdateProvider(type=RoleSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Role record);

    @Update({
        "update t_role",
        " set code = #{code,jdbcType=VARCHAR},",
          " text = #{text,jdbcType=VARCHAR},",
          " enabled = #{enabled,jdbcType=BIT},",
          " description = #{description,jdbcType=VARCHAR},",
          " create_time = #{createTime,jdbcType=TIMESTAMP},",
          " modify_time = #{modifyTime,jdbcType=TIMESTAMP}",
        " where id = #{id,jdbcType=BIGINT}"
    })
    int updateByPrimaryKey(Role record);


    @Select("select * from t_role trole left join t_role_permission trp on trole.id=trp.role_id" +
            " LEFT JOIN t_permission tp on tp.id=trp.permission_id where trole.id in(#{ids})")
    @Lang(SimpleSelectInLangDriver.class)
    List<Role> selectInOfPermission(@Param("ids") List<Long> ids);


    @Update("update t_role set enabled=#{enabled} where id=#{id}")
    int enableById(@Param("id") Long id,@Param("enabled") Boolean enabled);

    @Select("select tr.*,tm.id as `menus.id`,tm.url as `menus.url`,tm.enabled as `menus.enabled` from (select * from t_role where id in(#{ids})) tr left join t_role_menu trm on tr.id=trm.role_id left join t_sys_menu tm on trm.menu_id=tm.id")
    @Lang(SimpleSelectInLangDriver.class)
    @ResultMap("BaseResultMap")
    List<Role> selectMenus(@Param("ids") List<Long> ids);
    @Select("select tr.*,tp.id as `permissions.id`,tp.url as `permissions.url`,tp.perm as `permissions.perm` from (select * from t_role where id in(#{ids}))tr  left join t_role_permission trp on tr.id=trp.role_id left join t_permission tp on tp.id=trp.permission_id")
    @Lang(SimpleSelectInLangDriver.class)
    @ResultMap("BaseResultMap")
    List<Role> selectPermissions(@Param("ids") List<Long> ids);
}